package org.infinitest.swingui;

import static org.infinitest.util.InfinitestSettings.*;

import java.awt.Rectangle;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;

class WindowBoundsManager extends WindowAdapter
{
  @Override
  public void windowClosing(WindowEvent event)
  {
    JFrame frame = (JFrame) event.getSource();
    Rectangle rect = frame.getBounds();
    setProperty(FRAME_BOUNDS_X, Integer.toString(rect.x));
    setProperty(FRAME_BOUNDS_Y, Integer.toString(rect.y));
    setProperty(FRAME_BOUNDS_WIDTH, Integer.toString(rect.width));
    setProperty(FRAME_BOUNDS_HEIGHT, Integer.toString(rect.height));
  }

  public static Rectangle getWindowBounds()
  {
    Rectangle defaultBounds = new Rectangle(0, 0, 600, 120);
    if (!getSettingsFile().exists())
      return defaultBounds;
    int x = Integer.parseInt(getProperty(FRAME_BOUNDS_X, defaultBounds.x));
    int y = Integer.parseInt(getProperty(FRAME_BOUNDS_Y, defaultBounds.y));
    int width = Integer.parseInt(getProperty(FRAME_BOUNDS_WIDTH, defaultBounds.width));
    int height = Integer.parseInt(getProperty(FRAME_BOUNDS_HEIGHT, defaultBounds.height));
    return new Rectangle(x, y, width, height);
  }
}
